//axios配置
import axios from 'axios'
import store from '@/store'

class HttpRequest {
    constructor(baseURL){
        this.baseURL = baseURL;
    }
    getInsideConfig() {     //获取默认配置
        const config = {
            baseURL: this.baseURL,
            headers: {
                'Content-Type' : 'application/x-www-form-urlencoded'
            }
        }
        return config
    }
    interceptors(instance, url) {
        // 请求拦截
        instance.interceptors.request.use(config => {
            return config
        }, error => {
            return Promise.reject(error)
        })
        // 响应拦截
        instance.interceptors.response.use(res => {
            const { data, status } = res
            return { data, status }
        }, error => {
            let errorInfo = error.response
            if (!errorInfo) {
                const { request: { statusText, status }, config } = JSON.parse(JSON.stringify(error))
                errorInfo = {
                    statusText,
                    status,
                    request: { responseURL: config.url }
                }
            }
            //addErrorLog(errorInfo)
            return Promise.reject(error)
        })
    }
    request(options){
        const instance = axios.create();
        options = Object.assign(this.getInsideConfig(),options);
        this.interceptors(instance,options.url);
        return instance(options);
    }
}

export default HttpRequest

